import 'package:flutter/material.dart';
import 'package:flutter22/pages/homes/news.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

import '../alumni.dart';
import '../campus.dart';
import '../info.dart';
import '../partner.dart';
import '../person.dart';
import '../production.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}

//按钮类
class IconContainer extends StatelessWidget {
  Color color;
  double size;
  IconButton iconButton;
  String text;
  IconContainer(this.iconButton, this.text,
      {Key? key, this.color = Colors.blue, this.size = 32})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(100.0),
            color: color,
          ),
          height: 40,
          width: 40,
          child: Center(child: iconButton),
        ),
        const SizedBox(
          height: 12,
        ),
        Text(
          text,
          textAlign: TextAlign.center,
          style: const TextStyle(fontSize: 10),
        )
      ],
    );
  }
}

class _HomePageState extends State<HomePage> {
  List<Map> imgList = [
    {'url': 'assets/images/bac1.jpeg'},
    {'url': 'assets/images/bac2.jpeg'},
    {'url': 'assets/images/bac3.jpeg'}
  ];
  final TextEditingController _textController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    //轮播图
    Widget mySwiperWidget() {
      return Swiper(
          itemBuilder: (context, index) {
            return Image.asset(
              imgList[index]['url'],
              fit: BoxFit.fill,
            );
          },
          itemCount: imgList.length,
          pagination: const SwiperPagination(),
          autoplay: true);
    }

    //左侧抽屉
    Widget drawer() {
      return Drawer(
        child: Column(
          children: [
            Row(
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                const Expanded(
                    flex: 1,
                    child: UserAccountsDrawerHeader(
                      accountName: Text("动画1班任同学"),
                      accountEmail: Text("ren.yuesheng@163.com"),
                      currentAccountPicture: CircleAvatar(
                          backgroundImage:
                              AssetImage('assets/images/school.jpg')),
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              fit: BoxFit.cover,
                              image: AssetImage('assets/images/head1.jpeg'))),
                    ))
              ],
            ),
            const ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.people),
              ),
              title: Text("个人中心"),
            ),
            const Divider(),
            const ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.settings),
              ),
              title: Text("系统设置"),
            ),
            const Divider(),
          ],
        ),
      );
    }

    //输入框
    Widget myInput() {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(100), color: Colors.white),
            height: 40,
            width: 40,
            child: Center(
                child: IconButton(
              icon: const Icon(
                Icons.person,
                color: Colors.blue,
              ),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
            )),
          ),
          const SizedBox(
            width: 15,
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(50),
            ),
            child: SizedBox(
              width: 220,
              height: 40,
              child: TextFormField(
                controller: _textController,
                autofocus: false,
                maxLines: null,
                readOnly: false,
                keyboardType: TextInputType.text,
                style: const TextStyle(color: Colors.blue),
                decoration: InputDecoration(
                    contentPadding: const EdgeInsets.symmetric(
                        horizontal: 20, vertical: 10),
                    //prefix: const Icon(Icons.search),
                    hintText: '请输入创业/工作室信息',
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(50))),
              ),
            ),
          ),
          /* const SizedBox(
            width: 1,
          ), */
          TextButton(
              onPressed: () {},
              child: const Text(
                '搜索',
                style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
              ))
        ],
      );
    }

    final size = MediaQuery.of(context).size;
    final appwith = size.width;
    final appheight = size.height;
    var arr = [
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.map,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const NewsPage();
            })));
          },
        ),
        '校园创业',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.more,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const NewsPage();
            })));
          },
        ),
        '社会创业',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.message,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const InfoPage();
            })));
          },
        ),
        '信息发布',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.outlined_flag,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const PersonPage();
            })));
          },
        ),
        '人才招募',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.pets,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const CampusPage();
            })));
          },
        ),
        '校园风采',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.photo,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const ProductionPage();
            })));
          },
        ),
        '作品展示',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.turned_in,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const PartnerPage();
            })));
          },
        ),
        '校企合作',
        color: Colors.blue,
      ),
      IconContainer(
        IconButton(
          icon: const Icon(
            Icons.people,
            color: Colors.white,
          ),
          onPressed: () {
            //路由跳转
            Navigator.of(context).push(MaterialPageRoute(builder: ((context) {
              return const AlumniPage();
            })));
          },
        ),
        '校友之家',
        color: Colors.blue,
      ),
    ];
    return Scaffold(
        drawer: drawer(),
        body: SingleChildScrollView(
          child: Center(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Stack(
                children: [
                  SizedBox(
                    height: 250,
                    width: double.infinity,
                    child: mySwiperWidget(),
                  ),
                  Positioned(
                    bottom: 140,
                    left: (appwith - 320) / 2,
                    child: myInput(),
                  )
                ],
              ),
              Container(
                height: 230,
                width: double.infinity,
                decoration: const BoxDecoration(
                  color: Color.fromARGB(248, 243, 240, 240),
                ),
                child: GridView.count(
                  crossAxisCount: 4,
                  mainAxisSpacing: 10,
                  //crossAxisSpacing: 20,
                  childAspectRatio: 1.2,
                  children: arr,
                ),
              ),
              Card(
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(10),
                        topRight: Radius.circular(10))),
                elevation: 0,
                margin: const EdgeInsets.all(5),
                child: SizedBox(
                  height: appheight - 550,
                  width: double.infinity,
                  child: Container(),
                ),
              )
            ],
          )),
        ));
  }
}
